using System;
using UnityEngine;


public class RelicView : MonoBehaviour, IBagItemView
{
    [Header("Relic 数据")]
    public Relic relic;
    public RelicEffectSystem relicEffectSystem;

    [Header("Animator & 材质")]
    [SerializeField] private Animator animator;
    [SerializeField] private SpriteRenderer backgroundOutline;
    [SerializeField] private Material defaultMaterial;
    [SerializeField] private Material outlineMaterial;

    // 高亮状态
    private bool _isHighlighted;
    public bool IsHighlighted
    {
        get => _isHighlighted;
        set
        {
            if (_isHighlighted == value) return;
            _isHighlighted = value;
            if (backgroundOutline != null)
                backgroundOutline.material = _isHighlighted ? outlineMaterial : defaultMaterial;
        }
    }

    // 接口实现
    public BagItemData BagItemData
    {
        get => relic;
        set
        {
            if (value is Relic newRelic)
            {
                SetupInternal(newRelic, EventMode.Default);
            }
        }
    }

    public EventMode EventMode { get; private set; } = EventMode.Default;

    private void Awake()
    {
        if (backgroundOutline != null)
            backgroundOutline.material = defaultMaterial;
        IsHighlighted = false;
    }

    #region Setup

    public void Setup(BagItemData data)
    {
        if (data is Relic newRelic)
            SetupInternal(newRelic, EventMode.Default);
    }

    public void Setup(BagItemData data, EventMode mode)
    {
        if (data is Relic newRelic)
            SetupInternal(newRelic, mode);
    }

    private void SetupInternal(Relic relicc, EventMode mode)
    {
        relic = relicc;
        EventMode = mode;

        CreateRelicEffectSystem();
        InitializeAnimatorController();
        relicEffectSystem?.OnEnableDo();
        Refresh();
    }

    public void Refresh()
    {
        // TODO: 刷新 UI，例如更新文本、图标等
    }

    #endregion

    private void OnDisable()
    {
        relicEffectSystem?.OnDisableDo();
    }

    #region 动画与效果

    private void CreateRelicEffectSystem()
    {
        if (relic == null) return;

        string classTitle = relic.Title + "RelicEffectSystem";
        Type type = Type.GetType(classTitle);

        if (type != null)
            relicEffectSystem = Activator.CreateInstance(type) as RelicEffectSystem;
        else
            Debug.LogWarning($"RelicEffectSystem 类型 {classTitle} 未找到");
    }

    private void InitializeAnimatorController()
    {
        if (relic == null || animator == null) return;

        string path = $"Relic/{relic.Title}/{relic.Title}_AC";
        RuntimeAnimatorController controller = Resources.Load<RuntimeAnimatorController>(path);
        if (controller != null)
            animator.runtimeAnimatorController = controller;
        else
            Debug.LogWarning($"Animator Controller 未找到: {path}");
    }

    #endregion

    #region 鼠标事件

    private void OnMouseEnter()
    {
        if (EventMode == EventMode.Default)
            DefaultOnMouseEnter();
        else
            CustomOnMouseEnter();
    }

    private void OnMouseExit()
    {
        if (EventMode == EventMode.Default)
            DefaultOnMouseExit();
        else
            CustomOnMouseExit();
    }

    private void OnMouseDown()
    {
        if (EventMode == EventMode.Default)
            DefaultOnMouseDown();
        else
            CustomOnMouseDown();
    }

    #endregion

    #region Default逻辑（类似原MapScene）

    private void DefaultOnMouseEnter()
    {
        IsHighlighted = true;
    }

    private void DefaultOnMouseExit()
    {
        IsHighlighted = false;
    }

    private void DefaultOnMouseDown()
    {
        BagSelectedGA ga = new BagSelectedGA(this);
        ActionSystem.Instance.AddReaction(ga);
    }

    #endregion

    #region Custom逻辑（自定义背包逻辑）

    private void CustomOnMouseEnter()
    {
        // Debug.Log($"{relic.Title}: 鼠标进入");
        IsHighlighted = true;
    }

    private void CustomOnMouseExit()
    {
        // Debug.Log($"{relic.Title}: 鼠标退出");
        IsHighlighted = false;
    }

    private void CustomOnMouseDown()
    {
        // Debug.Log($"{relic.Title}: 鼠标点击");
        BagSelectedGA ga = new BagSelectedGA(this);
        ActionSystem.Instance.AddReaction(ga);
    }

    #endregion
}
